package com.easytopit.demo.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easytopit.demo.domain.Movie;
import com.easytopit.demo.domain.Order;
import com.easytopit.demo.domain.enumDomain.Version;
import com.easytopit.demo.service.MovieService;
import com.easytopit.demo.service.OrderService;
import com.easytopit.demo.service.UserService;

@Controller
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private MovieService movieService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/addMoney")
	public String addMoneyPage(){
		return "movie/addMoney";
	}
	
	@RequestMapping(value = "/addMoney.do",method = RequestMethod.GET)
	public String addMoney(String money,HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		if(name!=null){
			double rmb = Double.valueOf(money);
			orderService.addMoney(name, rmb);
			return "index/userIndex";
		}
		modelMap.put("mes", "尚未登录");
		return "user/login";
	}
	
	@RequestMapping(value = "/selectMovie")
	public String selectMoviePage(ModelMap modelMap){
		List<String> movie =  movieService.queryMovieByStatus("正在上映");
		List<Movie> movies = new LinkedList<>();
		for(int i = 0; i < movie.size(); i++){
			movies = movieService.queryMovieByName(movie.get(i));
		}
		modelMap.put("movies", movies);
		return "order/selectMovie";
	}
	
	@RequestMapping(value = "/selectMovie.do",method = RequestMethod.POST)
	public String selectMovie(String movie,Timestamp date,String cinema,String time,ModelMap modelMap){
		modelMap.put("movie", movie);
		modelMap.put("cinema", cinema);
		modelMap.put("hall", "6号厅");
		modelMap.put("time", time);
		return "order/selectSeat";
	}
	
	@RequestMapping(value = "/selectSeat.do",method = RequestMethod.POST)
	public String selectSeat(String cinema,String hall,String movie,String version,String time,String seat,HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		Random random = new Random();
		String num = String.valueOf(random.nextInt(10000000));
		Double price = movieService.queryPriceByVersion(movie, version);
		String[] seats = seat.split(", ");
		List<String> place = new LinkedList<>();
		for(int i = 0; i < seats.length; i++){
			place.add(seats[i]);
		}
		Timestamp date = new Timestamp(new Date().getTime());
		Integer quantity = seats.length;
		Double money = quantity*price;
		String t = String.valueOf(date);
		orderService.addOrder(num,name,cinema,hall,movie,time,version,"语言",place.toString(), quantity,price,money,t,t,"待处理","未支付");
		modelMap.put("num", num);
		modelMap.put("createTime", t);
		modelMap.put("movieTime", t);
		modelMap.put("cinema", cinema);
		modelMap.put("hall", hall);
		modelMap.put("quantity", quantity);
		modelMap.put("money", money);
		modelMap.put("movieName", movie);
		modelMap.put("info", place);
		return "order/checkInfo";
	}
	
	@RequestMapping(value = "/checkInfo.do",method = RequestMethod.GET)
	public String checkInfo(String id,HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		Order orderInfo = orderService.queryOrderById(id);
		if(orderInfo!=null){
			if(!orderInfo.getPayStatus().equals("已退订")){
				if(orderService.userBalance(name) > orderInfo.getMoney()){
					orderService.reduceBalance(name, orderInfo.getMoney());
					orderService.payOrder(orderInfo.getId(), String.valueOf(new Timestamp(new Date().getTime())));
					modelMap.put("mes", "alert('支付成功');");
					return "index/userIndex";
				}
				modelMap.put("mes", "alert('余额不足,请及时充值');");
				return "order/checkInfo";
			}
			modelMap.put("mes", "alert('该订单已失效');");
			return "order/checkInfo";
		}
		modelMap.put("mes", "alert('订单不存在');");
		return "order/checkInfo";
	}
	
	@RequestMapping(value = "/orderIndex")
	public String orderIndex(HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		List<Order> orders = orderService.queryOrderByName(name);
		modelMap.put("name", name);
		modelMap.put("passwd", userService.login(name));
		modelMap.put("orders", orders);
		return "index/orderIndex";
	}
	
	@RequestMapping(value = "/refundOrder.do",method = RequestMethod.GET)
	public String refundOrder(String id,HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		List<Order> orders = orderService.queryOrderByName(name);
		modelMap.put("orders", orders);
		Order orderInfo = orderService.queryOrderById(id);
		if(orderInfo!=null){
			if(orderInfo.getPayStatus().equals("已支付")){
				orderService.addBalance(name, orderInfo.getMoney());
				orderService.refundOrder(id, String.valueOf(new Timestamp(new Date().getTime())));
				modelMap.put("mes", "alert('退订成功');");
				return "index/orderIndex";
			}else if(orderInfo.getPayStatus().equals("已退订")){
				modelMap.put("mes", "alert('该订单已退订');");
				return "index/orderIndex";
			}
			modelMap.put("mes", "alert('尚未支付该订单');");
			return "index/orderIndex";
		}
		modelMap.put("mes", "alert('订单不存在');");
		return "index/orderIndex";
	}
	
	@RequestMapping(value = "/searchOrder.do",method = RequestMethod.POST)
	public String searchOrder(String num,String payStatus,String createTime,String endTime,String cinema,HttpServletRequest request,HttpSession httpSession,ModelMap modelMap){
		String name = (String) httpSession.getAttribute("USER_NAME");
		List<Order> orders = orderService.searchOrder(name, num, payStatus, createTime, endTime, cinema);
		modelMap.put("orders", orders);
		return "index/orderIndex";
	}
}